#!/bin/bash

BASE_PATH=`pwd`
RESULT_PATH=${BASE_PATH}/result
JOB_NUM=1
J_FLAT=0
RESULT_FILE=${RESULT_PATH}/Total.result

if [[ -f ${RESULT_FILE} ]];then
	mv ${RESULT_FILE} ${RESULT_FILE}_`date +%F_%T`.bak
	touch ${RESULT_FILE}
fi

job=0
while [[ ${job} -lt ${JOB_NUM} ]]
do
        FLAG=0
	## rw_model:0 means single model;1 means mixed model
	rw_model=0
	for f in `ls ${RESULT_PATH} | grep result\\\\.log`
	do
		if [[ ${J_FLAG} -eq 0 ]]; then
			JOB_NUM=`cat ${RESULT_PATH}/$f | grep JOB | wc -l `
			J_FLAT=1
			if [[ ${JOB_NUM} -eq 0 ]];then
				echo -e  "\033[31;1m[ERROR] No result data to be proceed,quit! \033[0m";
				exit 10
			fi
		fi
		if [[ ${FLAG} -eq 0 ]];then
			title=`sed -n "/JOB_${job}/p"  ${RESULT_PATH}/$f | awk -F "_DISK" '{print $1}' `
			printf "====================     ${title}     ====================\n" >> ${RESULT_FILE}
			if [[ ${title} == *rw* ]];then
				rw_model=1
				printf "%-15s%15s%15s%15s%15s%15s%15s\n" "HOST_IP" "READ_MBPS" "READ_IOPS" "READ_LAT" "WRITE_MBPS" "WRITE_IOPS" "WRITE_LAT" >> ${RESULT_FILE}
			fi
                        if [[ ${title} == *read* ]];then
				printf "%-15s%15s%15s%15s\n" "HOST_IP" "READ_MBPS" "READ_IOPS" "READ_LAT" >> ${RESULT_FILE}
                        fi
                        if [[ ${title} == *write* ]];then
                                printf "%-15s%15s%15s%15s\n" "HOST_IP" "WRITE_MBPS" "WRITE_IOPS" "WRITE_LAT" >> ${RESULT_FILE}
                        fi
			FLAG=1
		fi
	        host=` echo $f | sed 's/_result.log//g' `
		temp=`sed -n "/JOB_${job}/,/JOB/p" ${RESULT_PATH}/$f | grep -v JOB | sed 's/io=[0-9]\+\.\?[0-9]\?[A-Z]\+//g;s/bw=//g;s/iops=//g;s/avg=//g' `
		unit1=`echo ${temp} | awk -F "lat" '{print $2}' | awk -F ":" '{print $1}' | sed 's/ec//g;s/\ //g' `
		unit2=`echo ${temp} | awk -F "lat" '{print $3}' | awk -F ":" '{print $1}' | sed 's/ec//g;s/\ //g' `
		printf "%-15s" ${host} >> ${RESULT_FILE}
		echo ${temp} | awk -v u1="${unit1}" -v u2="${unit2}" -F "," '{ printf "%15s%15s%15s%15s%15s%15s\n",$2,$3,$6u1,$8,$9,$12u2 } ' >> ${RESULT_FILE}
	done 
	sed -n "/JOB_${job}/,/JOB/p" ${RESULT_FILE}  | grep -v JOB | awk '
		BEGIN {f1=0;f2=0;f3=0;f4=0;f5=0;f6=0;} {
			if( $2 ~ /KB/ ) { tmp=substr($2,1,match($2,"K")-1 );f1+=(tmp/1024)  }
			if( $2 ~ /MB/ ) { tmp=substr($2,1,match($2,"M")-1 );f1+=tmp }
			f2+=$3;
			if( $4 ~ /us/ ) { tmp=substr($4,1,match($4,"u")-2 );f3+=(tmp/1000)  }
			if( $4 ~ /ms/ ) { tmp=substr($4,1,match($4,"m")-2 );f3+=tmp }
                	if( $5 ~ /KB/ ) { tmp=substr($5,1,match($5,"K")-1 );f4+=(tmp/1024)  }
                	if( $5 ~ /MB/ ) { tmp=substr($5,1,match($5,"M")-1 );f4+=tmp }
                	f5+=$6;
                	if( $7 ~ /us/ ) { tmp=substr($7,1,match($7,"u")-2 );f6+=(tmp/1000)  }
                	if( $7 ~ /ms/ ) { tmp=substr($7,1,match($7,"m")-2 );f6+=tmp }
		} 
		END { 
			if ( NF == 7 ) { printf "%-15s%15s%15s%15s%15s%15s%15s\n","TOTAL",f1"(MB/s)",f2,f3/(NR-1)"(ms)",f4"(MB/s)",f5,f6/(NR-1)"(ms)" } 
			if ( NF == 4 ) { printf "%-15s%15s%15s%15s\n","TOTAL",f1"(MB/s)",f2,f3/(NR-1)"(ms)" }  
		}' >>  ${RESULT_FILE}
	((job++))
done





